package com.fubang.pathlearning.canvas;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.RectF;
import android.support.annotation.Nullable;
import android.util.AttributeSet;
import android.view.View;

/**
 * 创建者: YIN
 * 创建时间: 2017/7/5 9:42
 * 电子邮箱: yin_ruimin@foxmail.com
 * 描述:画布平移，缩放，旋转，错切，存储状态
 */

public class CanvasView extends View {

    private Paint mBluePaint;
    private Paint mBlackPaint;
    private Paint mRedPaint;
    private int mHeight;
    private int mWidth;

    public CanvasView(Context context) {
        this(context, null);
    }

    public CanvasView(Context context, @Nullable AttributeSet attrs) {
        this(context, attrs, -1);
    }

    public CanvasView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        init();
    }

    private void init() {
        mBluePaint = new Paint();
        mBlackPaint = new Paint();
        mRedPaint = new Paint();

        mBluePaint.setColor(Color.BLUE);
        mBlackPaint.setColor(Color.BLACK);
        mRedPaint.setColor(Color.RED);

        mBluePaint.setAntiAlias(true);
        mBluePaint.setStyle(Paint.Style.STROKE);
        mBluePaint.setStrokeWidth(2f);

        mBlackPaint.setAntiAlias(true);
        mBlackPaint.setStyle(Paint.Style.STROKE);
        mBlackPaint.setStrokeWidth(2f);

        mRedPaint.setAntiAlias(true);
        mRedPaint.setStyle(Paint.Style.STROKE);
        mRedPaint.setStrokeWidth(20f);
    }

    @Override
    protected void onSizeChanged(int w, int h, int oldw, int oldh) {
        super.onSizeChanged(w, h, oldw, oldh);
        this.mWidth = w;
        this.mHeight = h;
    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        canvas.translate(mWidth / 2, mHeight / 2);
        RectF rect = new RectF(-400, -400, 400, 400);   // 矩形区域，设置画布尺寸
        for (int i = 0; i <= 20; i++) {
            canvas.scale(0.9f, 0.9f);
            canvas.drawRect(rect, mBlackPaint);
            canvas.drawCircle(0, 0, 100, mBluePaint);//画布缩到最小
        }
        canvas.drawCircle(0, 0, 1500, mRedPaint);
        canvas.drawCircle(0, 0, 1300, mRedPaint);
        for (int i = 0; i <= 360; i += 10) {               // 绘制圆形之间的连接线
            canvas.drawLine(0, 1500, 0, 1300, mRedPaint);
            canvas.rotate(10);
        }


//        RectF rect = new RectF(0,0,200,200);   // 矩形区域
//
//        mBluePaint.setColor(Color.BLACK);           // 绘制黑色矩形
//        canvas.drawRect(rect,mBluePaint);
//
//        canvas.skew(1,0);
//        canvas.skew(0,1);
//
//        mBluePaint.setColor(Color.BLUE);            // 绘制蓝色矩形
//        canvas.drawRect(rect,mBluePaint);

    }
}
